<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once('phpmailer/config.php');
require_once('phpmailer/MailClass.inc');
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class agent_email extends Page
{
       public $hfcomp_id = null;
       public $hfcomp_name = null;
       public $Image1 = null;
       public $JSChangeEmail = null;
       public $hfCompanyId = null;
       public $JTSiteTheme1 = null;
       public $JTContactPersons = null;
       public $imgShowContacts = null;
       public $memoMsg = null;
       public $edSubj = null;
       public $edCc = null;
       public $edTo = null;
       public $btnSubmit = null;
       public $Label2 = null;
       public $Label1 = null;
       public $Label50 = null;
       public $Image2 = null;
       function JTContactPersonsShow($sender, $params)
       {
        $query_contact = "SELECT * FROM company_contact WHERE company_id='".$this->hfCompanyId->getValue()."'";
        $result_contact = mysql_query($query_contact) or die("SELECT CONTACTS ".mysql_error());
        $data = array();
        while($row_contact = mysql_fetch_array($result_contact))
        {
         $radio = "<input type='radio' name='radio_email' onclick=changeEmail('".$row_contact['email']."'); />".$row_contact['fname']." ".$row_contact['lname'];
         $data[] = array('Name'=>$radio,
         'Email Address'=>$row_contact['email']);
        }
        $this->JTContactPersons->setCellData($data);
       }


       function agent_emailJSLoad($sender, $params)
       {

       ?>
       window.innerWidth = "525";

       document.getElementById('JTContactPersons_colitem_hdr_0').width = '215px';
       document.getElementById('JTContactPersons_colitem_hdr_1').width = '250px';

       document.getElementById('JTContactPersons_colitem_body_0').width = '215px';
       document.getElementById('JTContactPersons_colitem_body_1').width = '250px';
       <?php

       }

       function imgShowContactsJSClick($sender, $params)
       {

       ?>
       if(window.innerWidth=="525")
       {
       window.innerWidth = "1000";
       document.getElementById('imgShowContacts').title = "Hide Contact Persons";
       }
       else if(window.innerWidth=="1000")
       {
       window.innerWidth = "525";
       document.getElementById('imgShowContacts').title = "Show Contact Persons";
       }
       <?php

       }

       function btnSubmitClick($sender, $params)
       {

       $result = mysql_query("insert into email_tbl (company_id,
       company_name,
       email_add,
       subject,
       message)

       values ('".$this->hfcomp_id->getValue()."',
       '".$this->hfcomp_name->getValue()."',
       '".$this->edTo->Text."',
       '".$this->edSubj->Text."',
       '".$this->memoMsg->Text."')")or die("INSERT EMAIL - " . mysql_error());

       //EMAIL FUNCTION
       $To = $this->edTo->Text;
       $Cc = $this->edCc->Text;

        $mailer = new FreakMailer();
        $mailer->AddReplyTo('no-reply@journeytech.com.ph',"Journeytech");
        $mailer->Subject = $this->edSubj->Text;
        $mailer->IsHTML(true);
        $mailer->Body = $this->memoMsg->Text;
        $mailer->AltBody = $this->memoMsg->Text;
        $mailer->AddAddress($To, $To);
        $mailer->Send();
/*if(!$mailer->Send())
{
    echo 'There was a problem sending this mail!';
}
else
{
    echo 'Mail sent!';
}*/
        $mailer->ClearAddresses();
        $mailer->ClearAttachments();

       /*echo"<script language=javascript>
       alert('Message Sent!');
       </script>";*/
       $this->clr();


       }


       function clr()
       {
       $this->edSubj->Text='';
       $this->memoMsg->Lines = array();

       }


       function agent_emailCreate($sender, $params)
       {
       $comp_id = $_GET['company_id'];
       $this->hfCompanyId->setValue($comp_id);
       $result_comp = mysql_query("SELECT * FROM company_tbl WHERE company_id='".$comp_id."'")or die(mysql_error());
       $row_comp = mysql_fetch_array($result_comp);
       $email = $row_comp['general_email'];
       $comp_name = $row_comp['company_name'];
       $this->hfcomp_name->setValue($comp_name);
       $comp_id = $row_comp['company_id'];
       $this->hfcomp_id->setValue($comp_id);

       $this->edTo->Text = $email;
       //$this->edTo->Text = 'jtech_alex@yahoo.com';
       //$this->edCc->Text = 'jtech_dennis@yahoo.com';
       }


}

global $application;

global $agent_email;

//Creates the form
$agent_email=new agent_email($application);

//Read from resource file
$agent_email->loadResource(__FILE__);

//Shows the form
$agent_email->show();

?>